#include "Common.h"
#include "SystemV/AppLibSystemV_Msg.hpp"

#include <sys/ipc.h>

/**********************************************************
 * Function Name:        AppLibSystemV_MsgCreate
 * Description:          消息初始化
 * Parameters[in]:       IN CHAR *pcFilePath
 *                       INOUT SINT32 *piQueueId
 * Parameters[out]:      INOUT SINT32 *piQueueId
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.12
***********************************************************/
ULONG AppLibSystemV_MsgCreate(IN CHAR *pcFilePath, INOUT SINT32 *piQueueId)
{
    SINT32 iFd = 0;

    if ((pcFilePath == NULL) || (piQueueId == NULL))
    {
        LOG_ERROR("err: pcFilePath=%p piQueueId=%p\n", pcFilePath, piQueueId);
        return STAT_ERR_INVAILD_PARAM;
    }

    iFd = AppLibSystemV_Get(APP_LIB_SYSTEM_V_TYPE_MSG, pcFilePath);
    if(iFd < 0)
    {
        LOG_ERROR("err %d [%s]\n", errno, strerror(errno));
        return STAT_ERR_SYSTEM_V_MSG_GET;
    }

    *piQueueId = iFd;

    return STAT_SUCCESS;
}

/**********************************************************
 * Function Name:        AppLibSystemV_MsgDestory
 * Description:          消息注销
 * Parameters[in]:       INOUT SINT32 *piQueueId
 * Parameters[out]:      INOUT SINT32 *piQueueId
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.12
***********************************************************/
ULONG AppLibSystemV_MsgDestory(INOUT SINT32 *piQueueId)
{
    if ((piQueueId == NULL) || (*piQueueId < 0))
    {
        LOG_ERROR("Queue already destory\n");
        return STAT_ERR_INVAILD_PARAM;
    }

    AppLibSystemV_Ctl(APP_LIB_SYSTEM_V_TYPE_MSG, *piQueueId, IPC_RMID, NULL);

    *piQueueId = -1;

    return STAT_SUCCESS;
}

/**********************************************************
 * Function Name:        AppLibSystemV_MsgSend
 * Description:          消息发送
 * Parameters[in]:       IN SINT32 iQueueId
                         IN APP_LIB_SYSTEM_V_MSG_s *msg
 * Parameters[out]:      void
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2020.05.15
***********************************************************/
ULONG AppLibSystemV_MsgSend(IN SINT32 iQueueId, IN APP_LIB_SYSTEM_V_MSG_s *msg)
{
    return AppLibSystemV_MsgSnd(iQueueId, msg, IPC_NOWAIT);
}

/**********************************************************
 * Function Name:        AppLibSystemV_MsgSendWait
 * Description:          消息发送
 * Parameters[in]:       IN SINT32 iQueueId
                         IN APP_LIB_SYSTEM_V_MSG_s *msg
 * Parameters[out]:      void
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2020.05.15
***********************************************************/
ULONG AppLibSystemV_MsgSendWait(IN SINT32 iQueueId, IN APP_LIB_SYSTEM_V_MSG_s *msg)
{
    return AppLibSystemV_MsgSnd(iQueueId, msg, 0);
}

/**********************************************************
 * Function Name:        AppLibSystemV_MsgRecv
 * Description:          消息接收
 * Parameters[in]:       IN SINT32 iQueueId
                         INOUT APP_LIB_SYSTEM_V_MSG_s *msg
 * Parameters[out]:      INOUT APP_LIB_SYSTEM_V_MSG_s *msg
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.12
***********************************************************/
ULONG AppLibSystemV_MsgRecv(IN SINT32 iQueueId, INOUT APP_LIB_SYSTEM_V_MSG_s *msg)
{
    return AppLibSystemV_MsgRcv(iQueueId, msg, IPC_NOWAIT);
}

/**********************************************************
 * Function Name:        AppLibSystemV_MsgRecvWait
 * Description:          消息阻塞式接收
 * Parameters[in]:       IN SINT32 iQueueId
                         INOUT APP_LIB_SYSTEM_V_MSG_s *msg
 * Parameters[out]:      INOUT LIB_SYSTEM_V_MSG_s *msg
 * ReturnValue:          ULONG
 * Author:               liuke
 * Date:                 2024.04.12
***********************************************************/
ULONG AppLibSystemV_MsgRecvWait(IN SINT32 iQueueId, INOUT APP_LIB_SYSTEM_V_MSG_s *msg)
{
    return AppLibSystemV_MsgRcv(iQueueId, msg, 0);;
}